package com.glw.sso.platform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glw.sso.platform.common.util.TreeUtil;
import com.glw.sso.platform.domain.entity.Menu;
import com.glw.sso.platform.domain.mapper.MenuMapper;
import com.glw.sso.platform.domain.vo.MenuTreeVO;
import com.glw.sso.platform.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-10-31
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<MenuTreeVO> getUserMenuTree(String pin, String appId) {
        List<Menu> menus = menuMapper.getUserMenu(pin, appId);
        List<MenuTreeVO> treeList = menus.stream()
                // .filter(menu -> !menu.getUuid().equals(menu.getParentUuid()))
                .map(menu -> {
                    MenuTreeVO node = new MenuTreeVO();
                    node.setId(menu.getUuid());
                    node.setParentId(menu.getParentUuid());
                    node.setName(menu.getName());
                    return node;
                }).collect(Collectors.toList());

        return TreeUtil.bulid(treeList, 0);
    }
}
